﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 考勤信息管理
{
    public partial class Form1 : Form
    {
       private  List<SE> _listSe;
        private Dictionary<SE,Record> _DicRecored;
        public List<SE> ListSe { get => _listSe; set => _listSe = value; }
        public Dictionary<SE, Record> DicRecored { get => _DicRecored; set => _DicRecored = value; }

        public Form1()
        {
           
            InitializeComponent();
            MyDB db = new MyDB();
            ListSe = db.getSE();
            this.DicRecored = new Dictionary<SE, Record>();
            //绑定数据
            this.BindingData();
            

        }
        public void BindingData()
        {
            //视图里面编辑列 设置DataPropertyName设置列对应的属性
            this.dataGridView.DataSource = new BindingList<SE>(this.ListSe);
        }
        /// <summary>
        /// 增加员工
        /// </summary>
        /// <param name="se"></param>
        public void addSE(SE se)
        {
            this.ListSe.Add(se);
        }
        /// <summary>
        /// 通过索引删除员工信息
        /// </summary>
        /// <param name="i"></param>
        public void delSE(int i)
        {
            this.ListSe.RemoveAt(i);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            addForm df = new addForm(this);
            df.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            //获取选中行的索引
            int i=this.dataGridView.CurrentRow.Index;
            updateForm f = new updateForm(this, this.ListSe[i]);
            f.ShowDialog();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            DialogResult re=MessageBox.Show("确定删除？", "确定删除？", MessageBoxButtons.OKCancel);
            if (re == DialogResult.OK)
            {
                int i = dataGridView.CurrentRow.Index;
                this.ListSe.RemoveAt(i);
                this.BindingData();
              //  MessageBox.Show("删除成功！");
                
            }
        }

        private void 签到ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int i = this.dataGridView.CurrentRow.Index;
            SE tse = this.ListSe[i];
            if (DicRecored.ContainsKey(tse)==false)
            {
                Record r = new Record();
                r.Signined = true;
                r.SignInTime = DateTime.Now;
                DicRecored.Add(tse, r);
                MessageBox.Show("签到成功！");
            }
            else {
                if (DicRecored[tse].Signined == true)
                {
                    MessageBox.Show("不能重复签到");
                }
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string content=txtboxsearch.Text;
            if (content == null || content == "")
            {
                this.BindingData();
                return;
            }
            List<SE> tmpses = new List<SE>();
            //遍历 利用 InedxOf  模糊查询
            foreach (SE item in this.ListSe)
            {
                if (item.Id.ToString().IndexOf(this.txtboxsearch.Text) != -1 || item.Name.ToString().IndexOf(this.txtboxsearch.Text) != -1 || item.Sex.ToString().IndexOf(this.txtboxsearch.Text) != -1)
                {
                    tmpses.Add(item);
                }
            }
            this.dataGridView.DataSource = new BindingList<SE>(tmpses);

        }

        private void 签退ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int i = this.dataGridView.CurrentRow.Index;
            SE tse = this.ListSe[i];
            if (DicRecored.ContainsKey(tse) == false)
            {
                
                MessageBox.Show("还没有签到");
            }
            else
            {
                if (DicRecored[tse].SignOuted == true)
                {
                    MessageBox.Show("不能重复签退");
                }
                else {
                    
                    DicRecored[tse].SignOuted = true;
                    DicRecored[tse].SignOutTime = DateTime.Now;
                    MessageBox.Show("签退成功！");
                }
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            RecordForm rf = new RecordForm(this.DicRecored);
            rf.ShowDialog();
        }
    }
}
